package vlcjdemo;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.player.embedded.videosurface.CanvasVideoSurface;
import uk.co.caprica.vlcj.player.events.VideoOutputEventListener;

public class MyPlayer {

    private final JFrame frame;
    private final JPanel contentPane;
    private final Canvas canvas;
    private final MediaPlayerFactory mediaPlayerFactory;
    private final EmbeddedMediaPlayer mediaPlayer;
    private final CanvasVideoSurface videoSurface;
    static String movieFile = "";
    JLabel timeLabel;

    public static void main(final String[] args) {
        movieFile = "C:\\aa\\mov\\saved\\LSonia7.wmv";

        System.out.println("Playing movie with vlc");
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MyPlayer().start(movieFile);
            }
        });

    }

    public MyPlayer() {
        canvas = new Canvas();
        canvas.setBackground(Color.black);

        contentPane = new JPanel();
        contentPane.setBackground(Color.black);
        contentPane.setLayout(new BorderLayout());
        contentPane.add(canvas, BorderLayout.CENTER);

        frame = new JFrame("My vlcj player");
        frame.setContentPane(contentPane);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mediaPlayerFactory = new MediaPlayerFactory(new String[]{"--no-video-title-show"});

        mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
        videoSurface = mediaPlayerFactory.newVideoSurface(canvas);

        mediaPlayer.setVideoSurface(videoSurface);

        mediaPlayer.addVideoOutputEventListener(new VideoOutputEventListener() {

            @Override
            public void videoOutputAvailable(MediaPlayer mediaPlayer, boolean videoOutput) {
                if (videoOutput) {
                    Dimension size = mediaPlayer.getVideoDimension();
                    if (size != null) {
//            canvas.setSize(size.width, size.height);
//            frame.pack();
                    }
                }
            }
        });

        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                mediaPlayer.release();
                mediaPlayerFactory.release();
            }
        });

        timeLabel = new JLabel("time:");
        contentPane.add(timeLabel, BorderLayout.SOUTH);

        frame.setVisible(true);

        Runnable importMovie = new Runnable() {

            public void run() {
                while (true) {
                    try {
                        long time = mediaPlayer.getTime();
                        timeLabel.setText("time: " + time);
                        if (time > 55000) {
                            mediaPlayer.setTime(50000);
                        }
                    } catch (Exception x) {
                        x.printStackTrace();
                    }
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        };

        new Thread(importMovie).start();
    }

    private void start(final String args) {
        mediaPlayer.playMedia(args);
        mediaPlayer.setTime(50000);

    }
}